import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImagePickerPage extends StatefulWidget {
  final String title;

  ImagePickerPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ImagePickerPageState();
  }
}

class ImagePickerPageState extends State<ImagePickerPage> {
  File _image;

  Future getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() {
      _image = image;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Picker Example'),
      ),
      body: Column(
        children: <Widget>[
          _image == null ? Text('No image selected.') : Image.file(_image),
          RaisedButton(
            onPressed: getImage,
            child: Text('相机'),
          ), RaisedButton(
            onPressed: () {
              getImageFrom();
            }
            ,
            child: Text('相册'),
          ),
        ],
      ),
    );
  }
  void getImageFrom() async{
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    setState(() {
      _image = image;
    });
  }
}

